/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simplepushserver;

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import simplepushserver.gui.LogEntryTableModel;
import simplepushserver.gui.LogView;
import simplepushserver.gui.NoneModalPopup;
import simplepushserver.gui.Notifier;
import simplepushserver.gui.SettingsDialog;
import simplepushserver.gui.SimpleSettingsDialog;

/**
 *
 * @author Filip
 */
public class PusherTray {
    private static final SettingsHandler sHandler = new SettingsHandler();;
    public  static TrayIcon trayIcon;
    private final SimpleLogger logger;
    private final PushServer s;
    private final LogView logView;
    private final Notifier nl;

    public PusherTray(){
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        logger = new SimpleLogger("SimplePushServer.log");
        
        nl = new Notifier() {
            public void onNotification(String message) {
                trayIcon.displayMessage("", message, TrayIcon.MessageType.INFO);
            }
        };
        s = new PushServer(sHandler, logger, nl);
        logView = new LogView(logger);
        ActionListener LogViewListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                logView.setVisible(true);
            }
        };
        MenuItem logViewItem = new MenuItem("View log");
        logViewItem.addActionListener(LogViewListener);

        ActionListener SettingsListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SimpleSettingsDialog s = new SimpleSettingsDialog(sHandler);
                s.setVisible(true);
            }
        };
        MenuItem settingsItem = new MenuItem("Settings");
        settingsItem.addActionListener(SettingsListener);

        ActionListener aboutListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "This is version: r22, Zippyshare integration added, clickfull.exe removed, new settings dialog");
            }
        };
        MenuItem aboutItem = new MenuItem("About");
        aboutItem.addActionListener(aboutListener);

        ActionListener exitListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        };
        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(exitListener);
        
        Image image = null;
        try {
            SystemTray tray = SystemTray.getSystemTray();
            PopupMenu popup = new PopupMenu();
            
            URL u = new URL("http://feality.se/pusher.gif");
            image = Toolkit.getDefaultToolkit().getImage(u);
            trayIcon = new TrayIcon(image, "Pusher (Server)", popup);
            trayIcon.setImageAutoSize(true);
            tray.add(trayIcon);
            
            popup.add(logViewItem);
            popup.add(settingsItem);
            popup.add(aboutItem);
            popup.add(exitItem);
            nl.onNotification(sHandler.getStartMessage());
            s.start();
            
            synchronized (sHandler){
                while(true){
                    sHandler.wait();
                    nl.onNotification("Running on port "+sHandler.getServerPort());
                    s.setPort(sHandler.getServerPort());
                }
            }
        } catch (Exception ex) {
            p(ex.toString());
            ex.printStackTrace();
        }
    }
    public static void p(String t){
        new NoneModalPopup(t,"PushServer").start();
    }

    private static SettingsHandler getSettingsHandler() {
        return sHandler;
    }
}

